public class RobotRecursif extends Robot
{
	protected final int max_depth;
	
	public RobotRecursif(Labyrinthe lab, int max_depth)
	{
		super(lab, new int[]{Labyrinthe.UP, Labyrinthe.RIGHT, Labyrinthe.DOWN, Labyrinthe.LEFT});
		this.max_depth = max_depth;
	}
	
	public RobotRecursif(Labyrinthe lab)
	{
		this(lab, 20);
	}

	public int findExit()
	{
		//-1 is an invalid direction, 
		//so laby.go(-1) won't do anything
		return explore(-1, 0);
	}
	
	protected int explore(int dir, int depth)
	{
		super.move(dir);
		if (laby.isExit())
			return depth;
		else if (depth > max_depth)
			return -1; //not found
		else
		{
			int ret;
			for (int i=0; i<preferences.length; i++)
			{
				if (super.canVisit(preferences[i]))
				{
					if ((ret = explore(preferences[i], depth+1)) == -1)
					{
						//undo
						super.resetVisited();
						super.move(preferences[(i+2) % 4]);
					}
					else
						//found!
						return ret;
				}
			}
			return -1; //not found
		}
		//backtrack is done after the recursive call (cleaner)
	}
	
}
